/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sunyard.redission.demos.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Slf4j
@Controller
public class BasicController {
    @Resource
    private DistributedLockService distributedLockService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private HttpServletResponse response;
    @GetMapping("pubSubDemo")
    public void pubSubDemo() {
        distributedLockService.pubSubDemo();
    }
    @GetMapping("ee")
    @ExcelExport()
    public void ee() {
        log.info("开始执行");
        // 拿到档案保管情况统计模板
        InputStream resourceAsStream = this.getClass().getClassLoader()
                .getResourceAsStream("excelTemplate/stoInventoryArcPlan.xlsx");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        log.info("正式导出");
        FileUtils.writeAttachment(response, "stoInventoryArcPlan.xlsx", resourceAsStream);
    }
    @ExcelExport()
    @GetMapping("ww")
    public void ww() {
        System.out.println("000");
    }
    @GetMapping("watchdogDemo")
    public void watchdogDemo() {
        distributedLockService.watchdogDemo();
    }
    @GetMapping("reentrantLockDemo")
    public void reentrantLockDemo() {
        TimeUnit timeUnit = TimeUnit.SECONDS;
//        distributedLockService.reentrantLockDemo();
        try {
            Long key = redisTemplate.getExpire("key", TimeUnit.SECONDS);
            System.out.println(key);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    @GetMapping("set")
    public void set() {
        try {
            redisUtils.set("22","33",10000);
        } catch (Exception e) {
            log.info(String.valueOf(e));
        }
    }
    @GetMapping("get")
    public void get() {
        try {
            String s = redisUtils.get("22");
            log.info(s);
        } catch (Exception e) {
            log.info(String.valueOf(e));
        }
    }
}
